from rest_framework.views import APIView
from rest_framework.response import Response
from django.core.paginator import Paginator
from .models import *
from .ser import *
class NewAPIview(APIView):          # 添加
    def post(self,request):
        data=request.data
        ser=Newser(data=data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response(ser.data,status=201)
    def get(self,request):         # 分页
        new_id=request.GET.get('new_id')
        page=request.GET.get('page',1)
        pag=Paginator(new_id,2)
        pags=pag.get_page(page)
        ser=Newser(pags.object_list,many=True)
        return Response({
            'news':ser.data,
            'pages':pag.num_pages,
            'total':pag.count
        })
